using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.IO;

namespace LanguageInfo.Common.Utilities
{
    /// <summary>
    /// 描述：Log4net 帮助类
    /// 作者：路晓阳
    /// 时间：2010-06-05 4:04:34 PM
    /// <summary>
    public class Log4netHelper
    {
        private static ILog log;

        /// <summary>
        /// 初始化
        /// </summary>
        static Log4netHelper()
        {
            log = log4net.LogManager.GetLogger("log4net");
        }

        /// <summary>
        /// 更改log4net配置中Append节点日志文件路径名(新增子目录)
        /// </summary>
        /// <param name="directoryname">新增的子目录名称</param>
        /// <param name="appendername">log4net配置文件中的Append节点名称</param>
        public static void ChangeFileName(string directoryname, string appendername)
        {
            log4net.Repository.Hierarchy.Logger hierarchyLogger = log.Logger as log4net.Repository.Hierarchy.Logger;
            if (hierarchyLogger != null)
            {
                log4net.Appender.IAppender appender = hierarchyLogger.Parent.GetAppender(appendername);
                if (appender != null)
                {
                    log4net.Appender.RollingFileAppender rollingfileAppender = appender as log4net.Appender.RollingFileAppender;
                    if (rollingfileAppender != null)
                    {
                        FileInfo fileinfo = new FileInfo(rollingfileAppender.File);
                        rollingfileAppender.File = Path.Combine(Path.Combine(fileinfo.DirectoryName, directoryname), fileinfo.Name);
                        rollingfileAppender.ActivateOptions();
                    }
                }
            }
        }

        /// <summary>
        /// 写调试日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public static void Debug(string message, Exception ex)
        {
            log.Debug(message, ex);
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public static void Error(string message, Exception ex)
        {
            log.Error(message, ex);
        }

        /// <summary>
        /// 写严重错误日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public static void Fatal(string message, Exception ex)
        {
            log.Fatal(message, ex);
        }

        /// <summary>
        /// 写信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public static void Info(string message, Exception ex)
        {
            log.Info(message, ex);
        }

        public static void Info(object message)
        {
            log.Info(message.ToString(), null);
        }
    }
}
